package com.oscar.ehome.view.welcomeview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.oscar.ehome.MainActivity;
import com.oscar.ehome.R;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.view.BasePhotoAdapter;
import com.oscar.ehome.view.loginview.ApproveActivity;
import com.oscar.ehome.view.loginview.LoginActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/7/31
 * 引导页
 */

public class LeadActivity extends Activity implements ViewPager.OnPageChangeListener {

    /**
     * 引导页图片
     */
    @BindView(R.id.yd_viewpager)
    ViewPager ydViewpager;
    /**
     * 跳过
     */
    @BindView(R.id.skip_lay)
    RelativeLayout skipLay;
    /**
     * 底部白点
     */
    @BindView(R.id.ydy1)
    ImageView ydy1;
    /**
     * 底部白点
     */
    @BindView(R.id.ydy2)
    ImageView ydy2;
    /**
     * 底部白点
     */
    @BindView(R.id.ydy3)
    ImageView ydy3;
    /**
     * 底部白点
     */
    @BindView(R.id.ydy4)
    ImageView ydy4;

    int size = 0;//第几张引导页
    private List<Integer> all_list = new ArrayList<Integer>();
    BasePhotoAdapter adapter;
    int lead = 0;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_lead);
        ButterKnife.bind(this);
        initView();
    }

    /**
     * 初始数据
     */
    public void initView() {

        lead = getIntent().getIntExtra("lead", 0);

        ydViewpager.setOnPageChangeListener(this);

        all_list.add(R.mipmap.ydy1);
        all_list.add(R.mipmap.ydy2);
        all_list.add(R.mipmap.ydy3);
        all_list.add(R.mipmap.ydy4);

        adapter = new BasePhotoAdapter(LeadActivity.this, all_list);
        ydViewpager.setAdapter(adapter);
    }

    @OnClick({R.id.skip_lay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.skip_lay:
                SystemConfigFactory.getInstance(LeadActivity.this).getSystemConfig().setIsFirst(true);
                if (lead == 2) {
                    finish();
                } else {
                    Intent intent;
                    if (SystemConfigFactory.getInstance(LeadActivity.this).getSystemConfig().getLogin()) {//已登陆
                        if (SystemConfigFactory.getInstance(LeadActivity.this).getSystemConfig().getStatus().equals("1")) {//已认证
                            intent = new Intent(LeadActivity.this, MainActivity.class);
                            intent.putExtra("intent", 0);
                        } else {
                            intent = new Intent(LeadActivity.this, ApproveActivity.class);
                        }
                    } else {
                        intent = new Intent(LeadActivity.this, LoginActivity.class);
                    }
                    startActivity(intent);
                    finish();
                }
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                size = 1;
                ydy1.setImageResource(R.mipmap.page_indicator_focused);
                ydy2.setImageResource(R.mipmap.page_indicator_unfocused);
                ydy3.setImageResource(R.mipmap.page_indicator_unfocused);
                ydy4.setImageResource(R.mipmap.page_indicator_unfocused);
                break;
            case 1:
                size = 2;
                ydy1.setImageResource(R.mipmap.page_indicator_unfocused);
                ydy4.setImageResource(R.mipmap.page_indicator_unfocused);
                ydy3.setImageResource(R.mipmap.page_indicator_unfocused);
                ydy2.setImageResource(R.mipmap.page_indicator_focused);
                break;
            case 2:
                size = 3;
                ydy1.setImageResource(R.mipmap.page_indicator_unfocused);
                ydy2.setImageResource(R.mipmap.page_indicator_unfocused);
                ydy4.setImageResource(R.mipmap.page_indicator_unfocused);
                ydy3.setImageResource(R.mipmap.page_indicator_focused);
                break;
            case 3:
                size = 4;
                ydy1.setImageResource(R.mipmap.page_indicator_unfocused);
                ydy2.setImageResource(R.mipmap.page_indicator_unfocused);
                ydy4.setImageResource(R.mipmap.page_indicator_focused);
                ydy3.setImageResource(R.mipmap.page_indicator_unfocused);
                break;
            case 4:
                SystemConfigFactory.getInstance(LeadActivity.this).getSystemConfig().setIsFirst(true);
                Intent intent;
                if (lead == 2) {
                    finish();
                } else {
                    if (SystemConfigFactory.getInstance(LeadActivity.this).getSystemConfig().getLogin()) {//已登陆
                        if (SystemConfigFactory.getInstance(LeadActivity.this).getSystemConfig().getStatus().equals("1")) {//已认证
                            intent = new Intent(LeadActivity.this, MainActivity.class);
                            intent.putExtra("intent", 0);
                        } else {
                            intent = new Intent(LeadActivity.this, ApproveActivity.class);
                        }
                    } else {
                        intent = new Intent(LeadActivity.this, LoginActivity.class);
                    }
                    startActivity(intent);
                    finish();
                }
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
